using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using TrenitaliaWP7.Resources;
using Services.Menu;
using Utilities;
using System.Device.Location;
using Microsoft.Devices;
using System.Windows;
using Microsoft.Phone.Tasks;

namespace TrenitaliaWP7
{
    class OptionsMenu : OrientableScreen
    {
        //ArrayList<MyButton> strings;
        public static bool ESABLE_GPS = true;
        ArrayList<MyButton> list;
        MyButton title, orderLabel, orderArea, allowRunLabel, allowRunButton, autoUpdateLabel, autoUpdateArea;
        MyButton editLabel, clearLabel;
        MyButton editFavoritesLabel, clearFavoritesLabel;
        MyButton clearShortCutsLabel;
        MyButton vibrationLabel, vibrationButton;
        MyButton feedbackLabel, feedbackButton;
        MyButton showAlwaysTimeLabel, showAlwaysTimeButton;
        MyButton suggestionKeyboardLabel, suggestionKeyboardButton;
        MyButton completePathsLabel, completePathsButton;
        MyButton allowGPSLabel, allowGPSButton;
        MyButton hiddenLastElement;
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        ArrayList<String> orderType;
        ArrayList<String> autoUpdateType;
        SpriteFont font;
        Texture2D box, whiteTexture, switchOn, switchOff;
        MyButton suggestionButton;
        Texture2D confirm, circle;
        bool existsPopUp;
        Rectangle upperBox;//, lowerBox;
        ArrayList<String> favoritesStrings;
        ArrayList<String> favoritesStrings2;
        public bool isPrevMainMenu;
        GeoCoordinateWatcher GeoCoordinateWatcher = new GeoCoordinateWatcher();
        public OptionsMenu(Game game, bool isPrevMainMenu)
            : base(game)
        {
            this.isPrevMainMenu = isPrevMainMenu;
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            input_manager.OnDrag += on_drag;
            input_manager.OnVerticalDrag += on_drag;
            input_manager.OnHorizontalDrag += on_drag;
            input_manager.OnDragCompleted += on_drag_completed;
            orderType = new ArrayList<string>();
            autoUpdateType = new ArrayList<string>();
            orderType.Add(Strings.LastUse);
            orderType.Add(Strings.Alphabetic);
            autoUpdateType.Add(Strings.Off);
            autoUpdateType.Add(Strings.Auto1Minute);
            autoUpdateType.Add(Strings.Auto5Minute);
            autoUpdateType.Add(Strings.Auto10Minute);
            list = new ArrayList<MyButton>();
            if (StateManager.OptionsState.autoUpdateMode >= autoUpdateType.Count || StateManager.OptionsState.autoUpdateMode < 0)
                StateManager.OptionsState.autoUpdateMode = 0;

            if (StateManager.OptionsState.order >= orderType.Count || StateManager.OptionsState.order < 0)
                StateManager.OptionsState.order = 0;
            existsPopUp = false;
            favoritesStrings = new ArrayList<String>();
            favoritesStrings2 = new ArrayList<String>();
            UpdateFavoritesStrings();
            base.Initialize();

        }
        private void UpdateFavoritesStrings()
        {
            favoritesStrings.Clear();
            favoritesStrings2.Clear();
            var f = StateManager.Favorites.favorites;
            for (int i = 0; i < f.Count; i++)
            {
                favoritesStrings.Add(f[i].ToString(0));
                favoritesStrings2.Add(f[i].ToString(1));
            }
        }
        protected override void LoadContent()
        {
            font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            box = Game.Content.Load<Texture2D>("bar_box");
            whiteTexture = Game.Content.Load<Texture2D>("whitePixel");
            switchOn = Game.Content.Load<Texture2D>("switch_on");
            switchOff = Game.Content.Load<Texture2D>("switch_off");
            confirm = Game.Content.Load<Texture2D>("confirm");
            circle = Game.Content.Load<Texture2D>("circle");
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed && !existsPopUp)
            {

                Storage.SaveOptionsState(StateManager.OptionsState);
                Storage.SaveFavorites(StateManager.Favorites);
                if (isPrevMainMenu)
                    StateManager.SelectMainMenu();
                else
                    StateManager.ReOpenDetailsMenu();

            }

            if (velocity != 0 && !dragging)
            {
                if (velocity < 0)
                {
                    velocity += dt * 2;
                    if (velocity > 0)
                        velocity = 0;
                }
                else
                {
                    velocity -= dt * 2;
                    if (velocity < 0)
                        velocity = 0;
                }
                //if (Math.Abs(velocity) < 0.5f)
                //    velocity = 0;
                var delta_y = (int)velocity;


                if (list.Count > 0 && list[0].rectangle.Y > dragArea.Y)
                {
                    velocity = 0;
                    delta_y = dragArea.Y - list[0].rectangle.Y;
                }
                else if (list.Count > 0 && list[list.Count - 1].rectangle.Y + list[list.Count - 1].rectangle.Height < dragArea.Y + dragArea.Height)
                {
                    velocity = 0;

                    delta_y = dragArea.Y + dragArea.Height - list[list.Count - 1].rectangle.Y - list[list.Count - 1].rectangle.Height;
                    if (list[0].rectangle.Y + delta_y > dragArea.Y)
                    {
                        delta_y = dragArea.Y - list[0].rectangle.Y;
                    }
                }

                for (int i = 0; i < list.Count; i++)
                {
                    list[i].rectangle.Y += delta_y;
                    list[i].UpdateTextPos();
                }

            }

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            for (int i = 0; i < list.Count; i++)
                list[i].Draw(spriteBatch);
            spriteBatch.Draw(whiteTexture, upperBox, Color.Black);
            //spriteBatch.Draw(whiteTexture, lowerBox, Color.Black);
            title.Draw(spriteBatch);
            //orderArea.Draw(spriteBatch);
            //orderLabel.Draw(spriteBatch);
            //editLabel.Draw(spriteBatch);
            //clearLabel.Draw(spriteBatch);
            //allowRunButton.Draw(spriteBatch);
            //allowRunLabel.Draw(spriteBatch);
            //autoUpdateArea.Draw(spriteBatch);
            //autoUpdateLabel.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        //Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            if (StateManager.isTrial)
            {
                if (RectContaintsVect(Game1.trial_rectangle, tap_position))
                {
                    OpenMarket(Game1.APP_ID);
                    return;
                }
            }
            if (orderArea != null && orderArea.tapped_on(tap_position) && !upperBox.Intersects(orderArea.rectangle))
            {
                this.Enabled = false;
                input_manager.Enabled = false;
                StateManager.OpenItemListComponent(ChangeOrderType, abortSelector, Strings.Order, orderType);
                //StateManager.OpenEditListComponent(RemoveStation, abortSelector, Strings.EditItemList, orderType, Strings.ConfirmDelete);
            }
            else if (editLabel != null && editLabel.tapped_on(tap_position) && !upperBox.Intersects(editLabel.rectangle))
            {
                this.Enabled = false;
                input_manager.Enabled = false;
                StateManager.OpenEditListComponent(RemoveStation, abortSelector, Strings.EditItemList,
                    StateManager.OptionsState.order == 0 ? StateManager.OptionsState.stations1 : StateManager.OptionsState.stations2,
                    Strings.ConfirmDelete);
                //Game.Components.Remove(input_manager);
            }
            else if (autoUpdateArea != null && autoUpdateArea.tapped_on(tap_position) && !upperBox.Intersects(autoUpdateArea.rectangle))
            {
                this.Enabled = false;
                input_manager.Enabled = false;
                StateManager.OpenItemListComponent(ChangeAutoUpdateType, abortSelector, Strings.AutoUpdate, autoUpdateType);
                //StateManager.OpenEditListComponent(RemoveStation, abortSelector, Strings.EditItemList, orderType, Strings.ConfirmDelete);
            }
            else if (clearShortCutsLabel != null && clearShortCutsLabel.tapped_on(tap_position) && !upperBox.Intersects(clearShortCutsLabel.rectangle))
            {
                if (StateManager.OpenQuestionPopUp(Strings.ClearShortCutsList_Confirm))
                    ClearShortCutsList();
            }
            else if (clearLabel.tapped_on(tap_position) && !upperBox.Intersects(clearLabel.rectangle))
            {
                if (StateManager.OpenQuestionPopUp(Strings.ClearQuestion))
                    ClearList();
                //StateManager.OpenPopUp(ClearList, ClosePopUp, Strings.ClearQuestion, "", Strings.Yes, Strings.No, font, box, whiteTexture, Color.Black);
                //input_manager.Enabled = false;
                //existsPopUp = true;
                //Game.Components.Remove(input_manager);
            }
            else if (allowRunButton != null && allowRunButton.tapped_on(tap_position) && !upperBox.Intersects(allowRunButton.rectangle))
            {
                StateManager.OptionsState.allowRunOffScreen = !StateManager.OptionsState.allowRunOffScreen;
                if (StateManager.OptionsState.allowRunOffScreen)
                    allowRunButton.texture = switchOn;
                else
                    allowRunButton.texture = switchOff;
                /*StateManager.OpenErrorPopUp(ClosePopUp, Strings.AllowRun2, "", font, confirm, circle, whiteTexture, Color.Black);
                input_manager.Enabled = false;
                Game.Components.Remove(input_manager);
                existsPopUp = true;
                 * */
                MessageBox.Show(Strings.AllowRun2);
            }
            else if (vibrationButton.tapped_on(tap_position) && !upperBox.Intersects(vibrationButton.rectangle))
            {
                StateManager.OptionsState.vibration = !StateManager.OptionsState.vibration;
                if (StateManager.OptionsState.vibration)
                {
                    vibrationButton.texture = switchOn;
                    StateManager.StartVibration(new TimeSpan(0, 0, 0, 0, 400));
                }
                else
                {
                    vibrationButton.texture = switchOff;
                    Microsoft.Devices.VibrateController.Default.Stop();
                }
            }
            else if (suggestionKeyboardButton != null && suggestionKeyboardButton.tapped_on(tap_position) && !upperBox.Intersects(suggestionKeyboardButton.rectangle))
            {
                StateManager.OptionsState.suggestionKeyboard = !StateManager.OptionsState.suggestionKeyboard;
                if (StateManager.OptionsState.suggestionKeyboard)
                {
                    suggestionKeyboardButton.texture = switchOn;

                    //StateManager.StartVibration(new TimeSpan(0, 0, 0, 0, 500));
                }
                else
                {
                    suggestionKeyboardButton.texture = switchOff;
                }
            }
            else if (feedbackButton != null && feedbackButton.tapped_on(tap_position, false) && !upperBox.Intersects(feedbackButton.rectangle))
            {
                StateManager.OptionsState.feedback = !StateManager.OptionsState.feedback;
                if (StateManager.OptionsState.feedback)
                {
                    feedbackButton.texture = switchOn;
                    MyButton.ALLOW_VIBRATE = true;
                    //StateManager.StartVibration(new TimeSpan(0, 0, 0, 0, 500));
                }
                else
                {
                    MyButton.ALLOW_VIBRATE = false;
                    Microsoft.Devices.VibrateController.Default.Stop();
                    feedbackButton.texture = switchOff;
                }
            }
            else if (showAlwaysTimeButton.tapped_on(tap_position) && !upperBox.Intersects(showAlwaysTimeButton.rectangle))
            {
                StateManager.OptionsState.showAlwaysTimes = !StateManager.OptionsState.showAlwaysTimes;
                if (StateManager.OptionsState.showAlwaysTimes)
                    showAlwaysTimeButton.texture = switchOn;
                else
                    showAlwaysTimeButton.texture = switchOff;
            }
            else if (completePathsButton.tapped_on(tap_position) && !upperBox.Intersects(completePathsButton.rectangle))
            {
                StateManager.OptionsState.showCompletePaths = !StateManager.OptionsState.showCompletePaths;
                if (StateManager.OptionsState.showCompletePaths)
                    completePathsButton.texture = switchOn;
                else
                    completePathsButton.texture = switchOff;
            }
            else if (allowGPSButton != null && allowGPSButton.tapped_on(tap_position) && !upperBox.Intersects(completePathsButton.rectangle))
            {
                if (!StateManager.OptionsState.AllowGPS)
                {
                    if (MessageBox.Show(Strings.GPS_Alert_Message, Strings.GPS_Alert_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        StateManager.OptionsState.AllowGPS = true;
                        //if (StateManager.OptionsState.AllowGPS)
                        //{
                        allowGPSButton.texture = switchOn;
                        //StateManager.LoadStations();
                        //}
                        //else
                        //    allowGPSButton.texture = switchOff;
                        if (StateManager.OptionsState.AllowGPS && (GeoCoordinateWatcher.Status == GeoPositionStatus.Disabled))
                        {
                            /*StateManager.OpenErrorPopUp(ClosePopUp, Strings.GPSOff, "", font, confirm, circle, whiteTexture, Color.Black);
                            input_manager.Enabled = false;
                            Game.Components.Remove(input_manager);
                            existsPopUp = true;
                             * */


                            MessageBox.Show(Strings.GPSOff);
                        }
                    }
                }
                else
                {
                    StateManager.OptionsState.AllowGPS = false;
                    allowGPSButton.texture = switchOff;
                }
            }
            else if (editFavoritesLabel != null && editFavoritesLabel.tapped_on(tap_position) && !upperBox.Intersects(editFavoritesLabel.rectangle))
            {
                this.Enabled = false;
                input_manager.Enabled = false;
                StateManager.OpenEditListComponent(RemoveFavorite, abortSelector, Strings.EditFavorites, favoritesStrings, favoritesStrings2, Strings.ConfirmDelete, font);
                //Game.Components.Remove(input_manager);
            }
            else if (editFavoritesLabel != null && clearFavoritesLabel.tapped_on(tap_position) && !upperBox.Intersects(clearFavoritesLabel.rectangle))
            {
                //StateManager.OpenPopUp(ClearFavoritesList, ClosePopUp, Strings.ClearQuestion, "", Strings.Yes, Strings.No, font, box, whiteTexture, Color.Black);
                //input_manager.Enabled = false;
                //existsPopUp = true;
                //Game.Components.Remove(input_manager);
                if (StateManager.OpenQuestionPopUp(Strings.ClearQuestion))
                    ClearList();
            }
            else if (suggestionButton != null && suggestionButton.tapped_on(tap_position))
            {
                //OpenKeyboard();
                try
                {
                    EmailComposeTask ect = new EmailComposeTask();

                    ect.Subject = "Informa Treni - " + Strings.Suggestion + " - " + DateTime.Now.ToString();
                    ect.To = "boscariol.francesco@gmail.com";

                    ect.Body = "Informa Treni - " + Strings.Suggestion + " - " + DateTime.Now.ToString() + "\n\n";

                    ect.Show();
                }
                catch (Exception e)
                {
                    e.ToString();
                }
            }
        }

        //void OpenKeyboard()
        //{
        //    Guide.BeginShowKeyboardInput(
        //           PlayerIndex.One,
        //           Strings.Suggestion,
        //           "",
        //           "",
        //           new AsyncCallback(OnEndShowKeyboardInput),
        //           null);
        //}
        //private void OnEndShowKeyboardInput(IAsyncResult result)
        //{
        //    if (result != null && Guide.EndShowKeyboardInput(result) != null)
        //    {
        //        SendSuggestion(Guide.EndShowKeyboardInput(result));
        //    }
        //}
        void ClearList()
        {
            StateManager.OptionsState.stations1.Clear();
            StateManager.OptionsState.stations2.Clear();
            //ClosePopUp();
        }
        void ClearShortCutsList()
        {

            StateManager.RebuildShortCuts();
            //ClosePopUp();
        }
        void ClearFavoritesList()
        {
            StateManager.Favorites.favorites.Clear();
            //ClosePopUp();
        }
        //void ClosePopUp()
        //{
        //    input_manager.Enabled = true;
        //    this.Enabled = true;
        //    Game.Components.Add(input_manager);
        //    existsPopUp = false;
        //    press_wait = 0;
        //}
        void ChangeOrderType(string newType)
        {
            if (newType == Strings.LastUse)
                StateManager.OptionsState.order = 0;
            else
                StateManager.OptionsState.order = 1;
            orderArea.text = newType;
            this.Enabled = true;
            input_manager.Enabled = true;
        }

        void ChangeAutoUpdateType(string newType)
        {
            if (newType == Strings.Off)
                StateManager.OptionsState.autoUpdateMode = 0;
            else if (newType == Strings.Auto1Minute)
                StateManager.OptionsState.autoUpdateMode = 1;
            else if (newType == Strings.Auto5Minute)
                StateManager.OptionsState.autoUpdateMode = 2;
            if (newType == Strings.Auto10Minute)
                StateManager.OptionsState.autoUpdateMode = 3;
            autoUpdateArea.text = newType;
            this.Enabled = true;
            input_manager.Enabled = true;
        }

        void RemoveStation(string s)
        {
            StateManager.OptionsState.RemoveStation(s);
            this.Enabled = true;
            input_manager.Enabled = true;
        }
        void RemoveFavorite(string s)
        {
            Details chosen = null;
            for (int i = 0; i < StateManager.Favorites.favorites.Count; i++)
                if (s == StateManager.Favorites.favorites[i].ToString())
                {
                    chosen = StateManager.Favorites.favorites[i];
                    break;
                }
            if (chosen != null)
            {
                StateManager.Favorites.RemoveFavorite(chosen);
                Storage.SaveFavorites(StateManager.Favorites);
            }
            this.Enabled = true;
            input_manager.Enabled = true;
            UpdateFavoritesStrings();
            //ClosePopUp();
        }
        void abortSelector()
        {
            this.Enabled = true;
            input_manager.Enabled = true;
        }

        Vector2 prev_drag_pos;
        bool dragging;
        float velocity;
        Rectangle dragArea;
        public bool RectContainsVect(Rectangle rect, Vector2 v)
        {
            return rect.X < v.X && rect.Y < v.Y && rect.X + rect.Width > v.X && rect.Y + rect.Height > v.Y;
        }
        void on_drag(Vector2 drag_pos)
        {
            if (prev_drag_pos != Vector2.Zero)
            {
                if (!RectContainsVect(dragArea, drag_pos))
                {
                    dragging = false;
                }
                else
                {
                    int offset = (int)(drag_pos.Y - prev_drag_pos.Y);
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].rectangle.Y += offset;
                        list[i].UpdateTextPos();
                    }
                    velocity = offset;
                }
            }
            else
            {
                if (RectContainsVect(dragArea, drag_pos))
                {
                    dragging = true;
                }

            }
            prev_drag_pos = drag_pos;
        }
        void on_drag_completed()
        {
            prev_drag_pos = Vector2.Zero;
            //if (velocity == 0)
            //    velocity = 0.01f;
            if (velocity == 0 && list.Count > 0 && list[list.Count - 1].rectangle.Y + list[list.Count - 1].rectangle.Height > dragArea.Y + dragArea.Height)
                velocity = 0.01f;
            dragging = false;
        }

        public override void SetPortraitOrientation()
        {
            int y = 100;
            int delta_y = 130;
            var titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");



            var s = StateManager.OptionsState.order == 0 ? Strings.LastUse : Strings.Alphabetic;

            title = new MyButton(new Rectangle(20, 0, 440, 100), Strings.Settings, Color.White, titleFont, TextPos.Left);
            upperBox = new Rectangle(0, 0, 480, 100);

            if (!StateManager.isTrial)
            {
                orderLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.Order, Color.White, font, TextPos.Left);
                orderArea = new MyButton(new Rectangle(20, y + 30, 440, 60), s, Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);

                y += delta_y;

                autoUpdateLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.AutoUpdate, Color.White, font, TextPos.Left);
                autoUpdateArea = new MyButton(new Rectangle(20, y + 30, 440, 60), autoUpdateType[StateManager.OptionsState.autoUpdateMode], Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);


                y += delta_y;
                allowRunLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.AllowRun1, Color.White, font, TextPos.Left);
                allowRunLabel.text2 = Strings.AllowRun2;
                allowRunLabel.font2 = font;
                allowRunLabel.textPos2 = TextPos.Left;
                allowRunLabel.textColor2 = Color.White;
                allowRunLabel.UpdateTextPos();
                allowRunButton = new MyButton(new Rectangle(350, y + 10, 100, 40), StateManager.OptionsState.allowRunOffScreen ? switchOn : switchOff, Color.White);
                y += delta_y;


                suggestionKeyboardLabel = new MyButton(new Rectangle(20, y - 10, 200, 60), Strings.SuggestionKeyboard, Color.White, font, TextPos.Left);
                suggestionKeyboardLabel.text2 = Strings.SuggestionKeyboard1;
                suggestionKeyboardLabel.font2 = font;
                suggestionKeyboardLabel.textPos2 = TextPos.Left;
                suggestionKeyboardLabel.textColor2 = Color.White;
                suggestionKeyboardLabel.UpdateTextPos();
                suggestionKeyboardButton = new MyButton(new Rectangle(350, y, 100, 40), StateManager.OptionsState.suggestionKeyboard ? switchOn : switchOff, Color.White);


                y += delta_y;

                feedbackLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.Feedback, Color.White, font, TextPos.Left);
                feedbackButton = new MyButton(new Rectangle(350, y - 10, 100, 40), StateManager.OptionsState.feedback ? switchOn : switchOff, Color.White);
                y += delta_y;
            }
            else
                y += 40;



            vibrationLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.Vibration, Color.White, font, TextPos.Left);
            vibrationButton = new MyButton(new Rectangle(350, y - 10, 100, 40), StateManager.OptionsState.vibration ? switchOn : switchOff, Color.White);


            y += delta_y;

            showAlwaysTimeLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.ShowAlwaysTimes, Color.White, font, TextPos.Left);
            showAlwaysTimeButton = new MyButton(new Rectangle(350, y - 10, 100, 40), StateManager.OptionsState.showAlwaysTimes ? switchOn : switchOff, Color.White);


            y += delta_y;

            completePathsLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.ShowCompletePaths, Color.White, font, TextPos.Left);
            completePathsButton = new MyButton(new Rectangle(350, y - 10, 100, 40), StateManager.OptionsState.showCompletePaths ? switchOn : switchOff, Color.White);

            if (!StateManager.isTrial)
            {
                if (ESABLE_GPS)
                {
                    y += delta_y;
                    allowGPSLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.AllowGPS, Color.White, font, TextPos.Left);
                    allowGPSButton = new MyButton(new Rectangle(350, y - 10, 100, 40), StateManager.OptionsState.AllowGPS ? switchOn : switchOff, Color.White);
                }
                y += delta_y;
                editFavoritesLabel = new MyButton(new Rectangle(20, y, 440, 60), Strings.EditFavorites, Color.White, font, TextPos.Left);
                y += delta_y;
                clearFavoritesLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.ClearFavorites, Color.White, font, TextPos.Left);


                y += delta_y;
                editLabel = new MyButton(new Rectangle(20, y, 440, 60), Strings.EditItemList, Color.White, font, TextPos.Left);
            }

            y += delta_y;
            clearLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.ClearList, Color.White, font, TextPos.Left);

            if (!StateManager.isTrial)
            {
                y += delta_y;
                clearShortCutsLabel = new MyButton(new Rectangle(20, y, 200, 60), Strings.ClearShortCutsList, Color.White, font, TextPos.Left);

                y += delta_y / 2;
                suggestionButton = new MyButton(new Rectangle(20, y + 30, 440, 60), Strings.Suggestion, Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);
            }
            y += delta_y;
            hiddenLastElement = new MyButton(new Rectangle(20, y, 440, 60), whiteTexture, Color.White * 0);
            if (StateManager.isTrial)
                dragArea = new Rectangle(0, 120, 480, 700);
            else
                dragArea = new Rectangle(0, 100, 480, 700);
        }
        protected override void ChangeOrientation()
        {
            base.ChangeOrientation();
            list.Clear();
            if (!StateManager.isTrial)
            {
                list.Add(orderLabel);
                list.Add(orderArea);
                list.Add(autoUpdateLabel);
                list.Add(autoUpdateArea);
            }
            if (allowRunLabel != null)
            {
                list.Add(allowRunLabel);
                list.Add(allowRunButton);
            }
            list.Add(vibrationLabel);
            list.Add(vibrationButton);

            if (!StateManager.isTrial)
            {
                list.Add(feedbackLabel);
                list.Add(feedbackButton);

                list.Add(suggestionKeyboardButton);
                list.Add(suggestionKeyboardLabel);
            }

            list.Add(showAlwaysTimeLabel);
            list.Add(showAlwaysTimeButton);
            list.Add(completePathsLabel);
            list.Add(completePathsButton);
            if (editFavoritesLabel != null)
            {
                list.Add(editFavoritesLabel);
                list.Add(clearFavoritesLabel);
            }
            if (allowGPSButton != null && ESABLE_GPS)
            {
                list.Add(allowGPSButton);
                list.Add(allowGPSLabel);
            }
            if (editLabel != null)
                list.Add(editLabel);
            list.Add(clearLabel);
            if (clearShortCutsLabel != null)
                list.Add(clearShortCutsLabel);
            if (suggestionButton != null)
                list.Add(suggestionButton);
            list.Add(hiddenLastElement);
        }
        public override void SetLandScapeOrientation()
        {
            int y = 100;
            int delta_y = 120;
            //var font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            var titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            //var box = Game.Content.Load<Texture2D>("bar_box");
            //var whiteTexture = Game.Content.Load<Texture2D>("whitePixel");
            var s = StateManager.OptionsState.order == 0 ? Strings.LastUse : Strings.Alphabetic;
            title = new MyButton(new Rectangle(100, 20, 800, 80), Strings.Settings, Color.White, titleFont, TextPos.Left);
            upperBox = new Rectangle(0, 0, 800, 80);

            if (!StateManager.isTrial)
            {

                orderLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.Order, Color.White, font, TextPos.Left);
                orderArea = new MyButton(new Rectangle(100, y + 30, 600, 60), s, Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);
                y += delta_y;
                autoUpdateLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.AutoUpdate, Color.White, font, TextPos.Left);
                autoUpdateArea = new MyButton(new Rectangle(100, y + 30, 600, 60), autoUpdateType[StateManager.OptionsState.autoUpdateMode], Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);


                y += delta_y;
                allowRunLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.AllowRun1, Color.White, font, TextPos.Left);

                allowRunLabel.text2 = Strings.AllowRun2;
                allowRunLabel.font2 = font;
                allowRunLabel.textPos2 = TextPos.Left;
                allowRunLabel.textColor2 = Color.White;
                allowRunLabel.UpdateTextPos();
                allowRunButton = new MyButton(new Rectangle(600, y + 10, 100, 40), StateManager.OptionsState.allowRunOffScreen ? switchOn : switchOff, Color.White);
                y += delta_y;


                suggestionKeyboardLabel = new MyButton(new Rectangle(100, y - 10, 200, 60), Strings.SuggestionKeyboard, Color.White, font, TextPos.Left);
                suggestionKeyboardLabel.text2 = Strings.SuggestionKeyboard1;
                suggestionKeyboardLabel.font2 = font;
                suggestionKeyboardLabel.textPos2 = TextPos.Left;
                suggestionKeyboardLabel.textColor2 = Color.White;
                suggestionKeyboardLabel.UpdateTextPos();
                suggestionKeyboardButton = new MyButton(new Rectangle(600, y, 100, 40), StateManager.OptionsState.suggestionKeyboard ? switchOn : switchOff, Color.White);


                y += delta_y;

                feedbackLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.Feedback, Color.White, font, TextPos.Left);
                feedbackButton = new MyButton(new Rectangle(600, y - 10, 100, 40), StateManager.OptionsState.feedback ? switchOn : switchOff, Color.White);


                y += delta_y;
            }
            vibrationLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.Vibration, Color.White, font, TextPos.Left);
            vibrationButton = new MyButton(new Rectangle(600, y - 10, 100, 40), StateManager.OptionsState.vibration ? switchOn : switchOff, Color.White);


            y += delta_y;



            showAlwaysTimeLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.ShowAlwaysTimes, Color.White, font, TextPos.Left);
            showAlwaysTimeButton = new MyButton(new Rectangle(600, y - 10, 100, 40), StateManager.OptionsState.showAlwaysTimes ? switchOn : switchOff, Color.White);


            y += delta_y;
            completePathsLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.ShowCompletePaths, Color.White, font, TextPos.Left);
            completePathsButton = new MyButton(new Rectangle(600, y - 10, 100, 40), StateManager.OptionsState.showCompletePaths ? switchOn : switchOff, Color.White);

            if (!StateManager.isTrial)
            {
                if (ESABLE_GPS)
                {
                    y += delta_y;
                    allowGPSLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.AllowGPS, Color.White, font, TextPos.Left);
                    allowGPSButton = new MyButton(new Rectangle(600, y - 10, 100, 40), StateManager.OptionsState.AllowGPS ? switchOn : switchOff, Color.White);
                }
                y += delta_y;
                editFavoritesLabel = new MyButton(new Rectangle(100, y, 440, 60), Strings.EditFavorites, Color.White, font, TextPos.Left);
                y += delta_y;
                clearFavoritesLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.ClearFavorites, Color.White, font, TextPos.Left);

                y += delta_y;
                editLabel = new MyButton(new Rectangle(100, y, 440, 60), Strings.EditItemList, Color.White, font, TextPos.Left);
            }

            y += delta_y;
            clearLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.ClearList, Color.White, font, TextPos.Left);

            if (!StateManager.isTrial)
            {
                y += delta_y;
                clearShortCutsLabel = new MyButton(new Rectangle(100, y, 200, 60), Strings.ClearShortCutsList, Color.White, font, TextPos.Left);

                y += delta_y / 2;
                suggestionButton = new MyButton(new Rectangle(100, y + 30, 600, 60), Strings.Suggestion, Color.Black, font, TextPos.Center, whiteTexture, Color.LightGray);
            }

            //editLabel = new MyButton(new Rectangle(100, 410, 200, 60), Strings.EditItemList, Color.White, font, TextPos.Left);

            //clearLabel = new MyButton(new Rectangle(500, 410, 200, 60), Strings.ClearList, Color.White, font, TextPos.Right);
            y += delta_y;
            hiddenLastElement = new MyButton(new Rectangle(100, y, 600, 60), whiteTexture, Color.White * 0);
            if (StateManager.isTrial)
                dragArea = new Rectangle(0, 140, 800, 380);
            else
                dragArea = new Rectangle(0, 100, 800, 380);

        }

        string FixExceptionString(string s)
        {
            string s1 = s.Replace("\r", "").Replace("\n", "");
            s1 = s1.Replace('\'', ' ').Replace('\"', ' ');

            return s1;
        }
        string FixDateString(string s)
        {
            string s1 = s.Replace('/', '-').Replace(':', '-').Replace(' ', '_');
            return s1;
        }
        void SendSuggestion(string s)
        {
            var url = "http://fboscari.altervista.org/submit_string.php/?tab=Suggerimenti&applicazione=InformaTreni&dataora=" +
                FixDateString(DateTime.Now.ToString()) + "&stringa=" + FixExceptionString(s);
            Utilities.getHTML(url, GotHTML, ErrorHTML);
            //MessageBox.Show();


        }
        void GotHTML(string s)
        {
            //
            //MessageBox.Show(Strings.Suggestion_Sent);
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { StateManager.OpenNotifyPopUp(Strings.Suggestion_Sent); });
        }
        void ErrorHTML()
        {
            //
            //MessageBox.Show(Strings.Suggestion_Fail);
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { StateManager.OpenNotifyPopUp(Strings.Suggestion_Fail); });
        }
    }
}
